package io.afu.baseframework.annotations.impls;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@Aspect
@Component
public class CacheJsonImpl {

    @Value("${cachePath:/home/www/region}")
    private String cachePath;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Pointcut(value = "@annotation(io.afu.baseframework.annotations.CacheJson)")
    public void cutCacheJson(){

    }

    @Around("cutCacheJson()")
    public Object cacheJsonImpl(ProceedingJoinPoint point) throws Throwable {
        Object result = point.proceed();
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null){
            // 如果获取到的不是请求的request 则直接过。
            return result;
        }
        HttpServletRequest request = requestAttributes.getRequest();
        HttpServletResponse response = requestAttributes.getResponse();
        if (cachePath == null ||cachePath.equals("")){
            // 如果没有设置路径，则直接过，不进行返回
            return result;
        }
        String uri = request.getRequestURI();
        String fullPath = cachePath + uri;
        return result;
    }



}
